import random
import sys
sys.path.insert(0, "/home/lighthouse/my-dream")
import hu_api
from module import config
import time
from module.dir import zhi_1, zhi_2, zhi_3, zhi_4, city_code, words, words_ch,words_list

class hu():
    #获取当前主页经典话题
    def get_answer(self):
        ress = hu_api.Hu_api().answer()
        # 获取当前日期时间
        now_time = times().get_now_time()
        # 保存文件
        save_file().save_txt(filename=zhi_1, text=now_time + "\n")
        #循环遍历里边的数值，并进行拼接
        for res in ress:
            final_data = res.find("span").text
            final_link = res.find("meta").get("content")
            final_news = "热门话题:" + final_data + " link:" + final_link + "\n"
            save_file().save_txt(filename=zhi_1, text=final_news)
            print("热门话题:" + final_data + " link:" + final_link)
    # 获取实时热点话题
    def get_cn_api(self):
        re_type, ress = hu_api.Hu_api().hot_topic()
        # i为标题的变量
        i = 1
        # 获取当前日期时间
        now_time = times().get_now_time()
        # 保存文件
        save_file().save_txt(filename=zhi_2, text=now_time + "\n")
        if re_type is True:
            # 循环遍历里边的数值，并进行拼接
            for res in ress["top_search"]["words"]:
                final_news = "<top" + str(i) + "> " + str(res["query"]) + " 话题： " + str(res["display_query"] + "\n")
                i = i + 1
                save_file().save_txt(filename=zhi_2, text=final_news)
                print(final_news)
        else:
            print("获取热点话题失败,请检查cookie")

    # 获取知乎日报
    def get_newest_news(self):
        res = hu_api.Hu_api().newest_news()
        ress = res["stories"]
        # 获取当前日期时间
        now_time = times().get_now_time()
        # 保存文件
        save_file().save_txt(filename=zhi_3, text=now_time + "\n")
        #print(res["stories"])
        for res in ress:
            # 循环遍历里边的数值，并进行拼接
            final_news = "标题: " + str(res["title"]) + " link：" + str(res["url"]) + "\n"
            save_file().save_txt(filename=zhi_3, text=final_news)
            print(final_news)
    # 获取热门消息
    def get_hot_news(self):
        res = hu_api.Hu_api().hot_news()
        ress = res["recent"]
        # 获取当前日期时间
        now_time = times().get_now_time()
        # 保存文件
        save_file().save_txt(filename=zhi_4, text=now_time + "\n")
        for res in ress:
            # 循环遍历里边的数值，并进行拼接
            final_news = "标题: " + str(res["title"]) + " link：" + str(res["url"]) + "\n"
            save_file().save_txt(filename=zhi_4, text=final_news)
            print(final_news)

# 创建一个保存文件的类，此类中为保存文件的不同方法
class save_file():
    # 保存txt文件
    def save_txt(self, filename, text):
        with open(filename, 'a+', encoding="utf-8") as f:
            f.write(text)
    # 读取城市code的txt文件内容
    def read_txt(self, city_name):

        filename = city_code
        # 读取文件信息
        with open(filename,  encoding="utf-8") as f:
            new_content = {}
            contents = f.readlines()
            # 将读取到的内容数据进行数据清洗
            for content in contents:
                content = content.strip('\n')
                content = content.lstrip()
                content = content.split(' ')
                # 判断数据是否是带有code
                if content[-1].isdigit() is True:
                    new_content[content[0]] = content[-1]
            if city_name in new_content:
                print("请稍等..")
                print(new_content[city_name])
                return new_content[city_name]
            else:
                city_name = input("请重新输入城市/返回主菜单(back)")
                if city_name == "back":
                    print("退出成功")
                    from weather_mid import weather_menu
                    weather_menu()
                else:
                    print("继续执行")
                    save_file().read_txt(city_name)

    def read_language_file(self,filename):
        with open(filename, encoding='utf-8') as f:
            contents = f.readlines()
            return contents
# 创建一个保存时间的类
class times():
    # 获取当前时间
    def get_now_time(self):
        return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))

class trans():
    # 随机进行抽取单词，解释该单词含义
    def en_to_ch(self):
        print("欢迎来到英-->中系统")
        import ast
        # 读取文件中的单词
        contents = save_file().read_language_file(words_list)
        # 获取文件中单词的数量
        print("系统中共有" + str(len(contents)) + "个单词")
        # 确认共随机的次数
        input_num = input("请输入你想背诵的单词次数")
        # 循环随机抽提的数量
        for i in range(0, int(input_num)):
            # 取列表中的随机索引
            random_num = random.randint(0, int(len(contents))-1)
            # 取随机索引中的key
            char_en = contents[random_num].split(":")[0].split("'")[1]
            input_chin = input("单词: " + char_en + " 释义: ")
            # 判断输入的答案与value是否一致
            if input_chin in str(ast.literal_eval(contents[random_num])[char_en]):
                print("回答正确")
            else:
                print("回答错误")
            print(ast.literal_eval(contents[random_num])[char_en])
